<template>
    <div class="address-list">      
        <van-address-list
            v-model="chosenAddressId"
            :list="addressList"
            default-tag-text="默认"
            @select="onSelect"
            @add="onAdd"
            @edit="onEdit"
            add-button-text="+添加地址"
        />
    </div>
</template>

<script>
import { Dialog, AddressList } from "vant";
import { getAddressList } from "@/api/address.js";
export default {
    components: {
        [AddressList.name]: AddressList,
    },
    data() {
        return {
            chosenAddressId: "",
            addressList: [],
            //showAdd: false
        };
    },
    created() {
        this.getAddressList();
    },
    methods: {
        onAdd() {
            this.$router.push({ name: "address_add" });
        }, //================================================

        //点击编辑地址
        onEdit(item) {
            this.$router.push({
                name: "address_edit",
                params: {
                    id: item.id,
                },
            });
        }, //================================================

        async getAddressList() {
            try {
                let result = await getAddressList(); //获取登录用户的收货地址
                
                if (result.code === 1) {                    

                    result.data.forEach((item) => {
                        let { id, name, tel } = item;
                        let address =
                            item.city + item.county + item.address_detail;
                        let isDefault = item.is_default == 1 ? true : false;
                        this.addressList.push({
                            id,
                            name,
                            tel,
                            address,
                            isDefault,
                        });
                        //默认选择
                        if (item.is_default == 1) {
                            this.active_id = item.id;
                        }
                    });
                } else if (result.code === 2) {
                    this.$router.push({ name: "login" });
                } else {
                    throw result.msg;
                }
            } catch (error) {
                Dialog({ title: "错误", message: error });
            }
        }, //================================================

        //点击列表
        onSelect(item) {
            let addressId = item.id;

            if (this.$route.name == "address_select") {
                this.$router.replace({
                    name: "order_create",
                    params: { addressId },
                });
            }
        },
    },
};
</script>

<style scoped lang="less">
@bgColor: #e0e4e9;
@mainColor: #919296;
.address-list {
    min-height: 100vh;
    background: @bgColor;

    /deep/.van-address-list {
        margin-top: 5px;
        .van-address-item .van-radio__icon--checked .van-icon {
            background: @mainColor;
            border-color: @mainColor;
        }
        .van-button--danger {
            background: @mainColor;
        }
    }
}
</style>
